<!-- 发文（函）公文详情-->
<template>
  <view>
    <van-collapse v-model="tableShow" accordion :border="border"
                  style="word-break: break-word !important;">
      <van-collapse-item title="基本信息" title-class="text-bold"  name="jbxx">
        <template #title>
          <view :class="tableShow == 'jbxx' ? 'content-blue':''">
            <i class="van-icon van-icon-label-o van-cell__left-icon"></i>
            <span class="van-cell__title text-bold">
              基本信息
            </span>
          </view>
        </template>
        <van-cell-group :border="border" v-if="this.util.isNotEmpty(detailObj1.detailObj)">
          <template v-if="detailObj1.detailObj.xzsplgw == '1'">
            <van-row class="content-border">
              <van-col span="8" class="content-title text-red flex align-center">行政许可类公文</van-col>
              <van-col span="16" class="content-value text-red">此件为行政审批事项<br>请加快办理速度！</van-col>
            </van-row>
            <van-row class="content-border">
              <van-col span="8" class="content-title">办结时间</van-col>
              <van-col span="16" class="content-value">{{ detailObj1.detailObj.endlineTime }}</van-col>
            </van-row>
          </template>
          <van-row class="content-border">
            <van-col span="8" class="content-title">标题</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.title}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">密级</van-col>
            <van-col span="16" class="content-value">{{this.dict.getDictNameByDictCode('mj',detailObj1.detailObj.secrecyLevel)}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">紧急程度</van-col>
            <van-col span="16" class="content-value content-red" v-if="this.util.isNotEmpty(detailObj1.detailObj.emergencyLevel) && detailObj1.detailObj.emergencyLevel != '1'">
              <i class="iconfont icon-dian"></i>{{this.dict.getDictNameByDictCode('jjcd',detailObj1.detailObj.emergencyLevel)}}
            </van-col>
            <van-col span="16" class="content-value" v-else>
              {{this.dict.getDictNameByDictCode('jjcd',detailObj1.detailObj.emergencyLevel)}}
            </van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">发文字号</van-col>
            <van-col span="16" class="content-value">
              <text>{{fwzhStr}}</text>
              <text class="color-red margin-left-xs text-bold" v-show="this.util.isNotEmpty(fwzhStr)">号</text>
            </van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">会签</van-col>
            <van-col span="16" class="content-value">{{signList.map(item=>{ return item.name+'('+item.state+')'}).toString()}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">主送</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.mainTo}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">抄送</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.copyTo}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">分送</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.partTo}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">拟稿</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.drafterDeptName}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">姓名</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.drafterName}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">电话</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.drafterPhone}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">是否公开</van-col>
            <van-col span="16" class="content-value">{{this.dict.getDictNameByDictCode('infopublic',detailObj1.detailObj.infoDisclosure)}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">印制份数</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.printFileNum}}</van-col>
          </van-row>
          <van-row class="content-border">
            <van-col span="8" class="content-title">拟稿说明</van-col>
            <van-col span="16" class="content-value">{{detailObj1.detailObj.description}}</van-col>
          </van-row>
        </van-cell-group>
      </van-collapse-item>
      <van-collapse-item title="正文及附件" title-class="text-bold" name="zwjfj">
        <template #title>
          <view :class="tableShow == 'zwjfj' ? 'content-blue':''">
            <i class="van-icon van-icon-font-o van-cell__left-icon"></i>
            <span class="van-cell__title text-bold">
              正文及附件
            </span>
          </view>
        </template>
        <view class="opinion" v-if="detailObj1.detailBody !=null && detailObj1.detailBody.doc.length > 0">
          <text class="opinion-left">正文</text>
          <view class="opinion-right">
            <!-- 多个文件默认显示第一个，并且显示文档修订记录按钮 -->
            <template v-for="(item,index) in detailObj1.detailBody.doc">
              <view :class="showRevisionBtn ? 'text-left' : 'text-right'" v-if="index==0">
                <text class="text-blue" @click="lx.openFile(item,detailObj1.detailObj.extendIdForRelate)">{{ item.attachName }}</text>
              </view>
            </template>
            <template v-if="showRevisionBtn">
              <van-button style="min-width: 30%" type="info" size="mini"
                          @click="showRevision(detailObj1.detailObj.extendIdForRelate)">文档修订记录
              </van-button>
            </template>
          </view>
        </view>

        <view class="opinion" v-if="detailObj1.detailAttachment != undefined &&detailObj1.detailAttachment.length>0 ">
          <text class="opinion-left">附件</text>
          <view class="opinion-right">
            <view v-for="(item,index) in detailObj1.detailAttachment" @click="lx.openFile(item,detailObj1.detailObj.extendIdForRelate)">
              <text class="text-blue">{{ item.attachName }}</text>
            </view>
          </view>
        </view>
        <view class="opinion" v-if="detailObj1.relation != undefined &&detailObj1.relation.length>0 ">
          <text class="opinion-left">附加公文</text>
          <view class="opinion-right">
            <view v-for="(item,index) in detailObj1.relation" @click="lx.openFile(item,detailObj1.detailObj.extendIdForRelate)">
              <text class="text-blue">{{ item.title }}</text>
            </view>
          </view>
        </view>
      </van-collapse-item>
      <van-collapse-item title="意见" title-class="text-bold" name="yj">
        <template #title>
          <view :class="tableShow == 'yj' ? 'content-blue':''">
            <i class="van-icon van-icon-comment-o van-cell__left-icon"></i>
            <span class="van-cell__title text-bold">
              意见
            </span>
          </view>
        </template>
        <view v-if="detailObj1.opinion != null">
          <!-- 只有发文有该意见，故做特殊处理 -->
          <template v-if="detailObj1.opinion['opinion_content_chushi'] != undefined">
            <view class="opinion"  >
              <view class="opinion-title">办公室审核</view>
              <view class="opinion-box padding-left-sm" v-for="item in detailObj1.opinion['opinion_content_chushi']">
                <view class="opinion-left">{{item.parentDeptName}}</view>
                <view class="opinion-left">{{item.opinion}}</view>
                <view class="opinion-right">
                  <!-- base64签名图片 -->
                  <img v-if="item.signature!='' && item.signature != undefined" :src="`data:image/png;base64,`+item.signature" alt="图片未上传" />
                  <text v-else class="opinion-user">{{item.userName}}</text>
                  <text class="opinion-date">{{item.approvalTime}}</text>
                </view>
              </view>
            </view>
          </template>

          <template v-for="(item,index) in reviewType">
            <view class="opinion"  v-if="detailObj1.opinion[item.dictItemCode] != undefined">
              <view class="opinion-title">{{item.dictItemName}}</view>
              <view class="opinion-box padding-left-sm" v-for="item in detailObj1.opinion[item.dictItemCode]">
<!--                <view class="opinion-left">{{item.parentDeptName}}</view>-->
                <view class="opinion-left">{{item.opinion}}</view>
                <view class="opinion-right">
                  <!-- base64签名图片 -->
                  <img v-if="item.signature!='' && item.signature != undefined" :src="`data:image/png;base64,`+item.signature" alt="图片未上传" />
                  <text v-else class="opinion-user">{{item.userName}}</text>
                  <text class="opinion-date">{{item.approvalTime}}</text>
                </view>
              </view>
            </view>
          </template>
        </view>
      </van-collapse-item>
      <!--公文分发功能-->
      <van-collapse-item v-if="showFenfaBtn" title="公文分发信息" title-class="text-bold" name="gwffxx">
        <template #title>
          <view :class="tableShow == 'gwffxx' ? 'content-blue':''">
            <i class="van-icon van-icon-share-o van-cell__left-icon"></i>
            <span class="van-cell__title text-bold">
                  公文分发信息
                </span>
          </view>
        </template>
        <van-row class="content-border">
          <van-col span="8" class="content-title">请选择分发人</van-col>
          <van-col span="16" class="content-value">
            <van-cell style="padding:0 !important;color: #969799" title="" :value="fenfaUserName" is-link
                      @click="showFenfa"/>
          </van-col>
        </van-row>
        <van-row class="content-border">
          <van-field style="min-width: 30% !important;" v-model="distributeInfo" center clearable rows="2" autosize
                     label="" type="textarea" placeholder="分发意见">
            <template #button>
              <van-button style="padding: 0 30rpx;!important;" size="mini" type="info" :disabled="!fenfaUserName"
                          @click="ditributetouser">分发
              </van-button>
              <van-button v-if="showYiyueBtn" style="padding: 0 26rpx;!important;" size="mini" type="info"
                          @click="updateDistributeStatus">已阅
              </van-button>
            </template>
          </van-field>
        </van-row>
        <view class="opinion">
          <view class="opinion-title">公文分发信息:</view>
          <view class="opinion-box padding-bottom" v-for="item in distributeProcess">
            <view class="opinion-left">分发过程:{{ item.distributeUserName }}--->{{
                item.receiveUserName
              }}({{ item.status == '0' ? '未阅' : '已阅:' + item.handleTime }})
            </view>
            <view class="opinion-left" v-if="item.distributeInfo">{{ item.distributeInfo }}</view>
            <view class="opinion-right">
              <text class="opinion-date">{{ item.distributeUserName }}</text>
              <text class="opinion-date">{{ item.distributeTime }}</text>
            </view>
          </view>
        </view>
      </van-collapse-item>
    </van-collapse>
  </view>
</template>

<script>
export default {
  props:{
    detailObj1:{
      detailObj:Object,
      detailBody:Object,
      detailAttachment:Object,//附件
      relation:Object,//附加公文[{"id":"1c0962f8839d46f792f84e5c89c96f14","title":"ppap","relateDocId":"4ec205b8334446f9b932d0fcb3896bc8","proInstId":"4ec205b8334446f9b932d0fcb3896bc8"},{"id":"2f4aa437ecba4c748bff911f03667147","title":"py","relateDocId":"86d30c881c494b468534685bb0746174","proInstId":"86d30c881c494b468534685bb0746174"}]
      opinion:Object,//意见
    },
    //列表中获取到的proInstId
    proInstId:{
      type:String,
      default: ''
    }

  },
  watch: {
    detailObj1: {
      handler(nVal, oVal) {
        if(this.util.isNotEmpty(nVal.detailObj)){
          //如果字头id不为空，则获取字头列表取出字头name（可能是pcbug，没有存字头名称，只存了id）
          if (this.util.isNotEmpty(nVal.detailObj.fwWordId)) {
            if(this.fwWordIdOld != nVal.detailObj.fwWordId) {
              this.fwWordIdOld = nVal.detailObj.fwWordId
              this.getGwWordList()
            }
          }
          if (this.util.isNotEmpty(nVal.detailObj.proInstId)) {
            if(this.proInstIdOld != nVal.detailObj.proInstId){
              this.proInstIdOld = nVal.detailObj.proInstId
              this.getSignList(this.proInstIdOld, nVal.detailObj.extendIdForRelate)
              this.getIsEditByProInstId(nVal.detailObj.extendIdForRelate)
              this.isProcessClosed(this.proInstIdOld)
            }
          }
        }
      },
      immediate: true,
      deep: true
    },
  },
  data() {
    return {
      fwzh:'',//发文字号串
      border: false,
      tableShow: "jbxx",
      message: '',
      reviewType:[],//意见类型
      infoDisclosure:{//字典值转换-是否公开
        1:"公开",
        2:"不公开",
        3:"主动公开"
      },
      signList: [], // 会签
      showRevisionBtn: false,//是否显示文档修订记录按钮
      fwWordIdOld:'',
      proInstIdOld:'',
      showFenfaBtn:false,//是否显示分发功能
      distributeInfo:'',//分发信息
      fenfaUserName:'',//选中的要分发的用户名，多个用顿号分割
      fenfaUserId:'',//选中的要分发的用户id，多个用逗号分割
      distributeProcess:[],//已分发的信息列表
      showYiyueBtn:false,//是否显示已阅按钮
    };
  },
  computed: {
    //发文字号串拼接
    fwzhStr() {
      if(this.util.isNotEmpty(this.fwzh)){
        if(this.util.isNotEmpty(this.detailObj1.detailObj.fwNum)){
          return this.fwzh + '〔'+this.detailObj1.detailObj.fwYear+'〕'+this.detailObj1.detailObj.fwNum
        }else{
          return this.fwzh
        }
      }else{
        return ''
      }
    },
  },
  mounted() {
    this.reviewType = this.cache.get('reviewType')
  },
  methods: {
    //判断是否显示分发功能
    showFenfa() {
      this.$emit('showFenfaSheet')
    },
    //父组件调用，传选中的用户对象给子组件
    initParam(userInfo){
      this.fenfaUserName = userInfo.userName.join('、')
      this.fenfaUserId = userInfo.userId.join(',')
    },
    //判断是否显示分发功能-判断流程是否结束，如果结束显示分发功能
    isProcessClosed(proInstId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/distributedoc/doc/isProcessClosed`),
        method: 'get',
        params: {
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'proInstId':proInstId
        }
      }).then(({data}) => {
        //返回true 则说明流程已结束，显示分送按钮{"code":200,"data":true,"message":null}
        if (data.code == 200) {
          this.showFenfaBtn = data.data
          //如果true，再调用获取公文分发信息接口
          this.getDistributeProcess(proInstId)
          this.isShowYiyueBtn(proInstId)
        }
      })
    },
    //已阅按钮是否显示
    isShowYiyueBtn(proInstId){
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/distributedoc/doc/getDistribute`),
        method: 'get',
        params: {
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'proInstId':proInstId
        }
      }).then(({data}) => {
        if (data.code == 200) {
          if(data.data.info.length>0){
            this.showYiyueBtn = true
          }else{
            this.showYiyueBtn = false
          }
        }
      })
    },
    //已阅
    updateDistributeStatus(){
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/distributedoc/doc/updateDistributeStatus`),
        method: 'get',
        params: {
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'proInstId': this.proInstIdOld
        }
      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast('操作成功')
          this.isShowYiyueBtn(this.proInstIdOld)
          this.getDistributeProcess(this.proInstIdOld)
        }else{
          this.$toast('已阅操作失败，错误码：'+data.code)
        }
      })
    },
    //获取已分发信息
    getDistributeProcess(proInstId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/distributedoc/doc/getDistributeProcess`),
        method: 'get',
        params: {
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'proInstId':proInstId
        }
      }).then(({data}) => {
        if (data.code == 200) {
          this.distributeProcess = data.data.process
        }else{
          this.$toast('获取公文分发信息失败，错误码：'+data.code)
        }
      })
    },
    //分发给分发人
    ditributetouser(){
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/distributedoc/doc/ditributetouser`),
        method: 'get',
        params: {
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'ids':this.fenfaUserId,
          'docType':this.detailObj1.detailObj.bizType,//文件类型
          'proInstId':this.proInstIdOld,
          'docId':this.detailObj1.detailObj.fwId,//文件id
          'title':this.detailObj1.detailObj.title,
          'distributeInfo':this.distributeInfo
        }
      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast('分发成功')
          this.getDistributeProcess(this.proInstIdOld)
          this.distributeInfo=''
          this.fenfaUserId=''
          this.fenfaUserName=''
        }else{
          this.$toast('分发出错了，错误码：'+data.code)
        }
      })
    },

    //判断是否显示文档修订记录按钮
    getIsEditByProInstId(proInstId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/component/maintext/getIsEditByProInstId/` + proInstId),
        method: 'get',
        params: {
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
        }
      }).then(({data}) => {
        if (data.code == 200) {
          if (data.data.num > 1) {
            this.showRevisionBtn = true
          }
        }
      })
    },
    //显示文档修订记录组件
    showRevision(proInstId) {
      this.$emit('showRevisionSheet', proInstId)
    },
    getGwWordList(){
      const _this = this
      this.$httploading({
        url: this.$http.adornUrl('/caac/oa/component/word/getwordbyuserandtype'),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          'type':'fw'

        })
      }).then(({data}) => {
        // console.log("获取字头=======data: " + JSON.stringify(data));
        if(data.code==200){
          if(this.util.isNotEmpty(data.data)){
            data.data.forEach((item)=>{
              if(item.wordId == this.detailObj1.detailObj.fwWordId){
                this.fwzh = item.wordName
                return
              }
              // _this.zt.push({name:item.wordName,wordId:item.wordId})
            })
          }
        }
      })
    },
    // 获取会签信息
    getSignList(proInstId,extendIdForRelate) {
      console.log("获取会签信息: " + extendIdForRelate);
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mhjcomponent/countersign/countersignController/countersign`),
        method: 'get',
        params: {
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'gongwenId': extendIdForRelate,
          'proInstId':proInstId
        }
      }).then(({data}) => {
        if (data.code == 200) {
          this.signList = data.data
        }
      })
    },
    //下载文件地址 或打开文件
    openFile(attachId){
      const _this = this
      this.$httploading({
        url: this.$http.adornUrl(`/caac/platform/UploadChunkController/download`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          'attachId':attachId
        })
      }).then(({data}) => {
        console.log("公文详情: " + JSON.stringify(data));
        if(data.code==200){
          console.log("下载文件成功");
        }else{
          this.$toast(data.message)
        }

      })
    }
  },
};
</script>

<style scoped>

/* 意见相关样式 */
.opinion{
  display: flex;
  flex-direction: column;
  margin: 0 32rpx;
  padding: 20rpx 0rpx;
  overflow: hidden;
  font-size: 28rpx;
  line-height: 24px;
  border-bottom: 1px solid #ebedf0;

}
.opinion .opinion-title{
  text-align: left;
  padding-bottom: 14rpx;
}
.opinion .opinion-box{
  margin-bottom: 20rpx;
}
.opinion .opinion-left{
  text-align: left;
  color: #323233;
}
.opinion .opinion-right{
  text-align: right;
  color: #969799;
}
.opinion .opinion-right .opinion-user{
  font-weight: 400;
  color: #000000;
}
.opinion .opinion-right .opinion-date{
  color: #323233;
  margin-left: 20rpx;
}
.van-cell__title {
  min-width: 50%;

  span {
    display: inline-block;
    text-align: left;
    word-break: break-all;
  }
}

/deep/ .van-collapse-item__content {
  padding: 0px;
  color: #969799;
  font-size: 14px;
  line-height: 1.5;
  background-color: #fff
}

/deep/ .content-border {
  border-bottom: 1rpx solid #e4e4e4;
  display: flex;
}
.content-title {
  background-color: #f2f2f2;
  padding: 10px 15px;
  color: #646566;
}
.content-value {
  padding: 10px 15px;
  text-align: right;
}
.content-red {
  color: #ed1c24;
}
.content-blue {
  color: #0081ff;
}

</style>
